package com.bsj.power.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.UserOperateLogSearchDTO;
import com.bsj.power.common.def.entity.userOperateLog.UserOperateLog;
import com.bsj.power.common.def.vo.sys.UserOperateLogVO;
import com.bsj.power.common.def.vo.sys.excel.PortalLoginLogExcelVO;
import com.bsj.power.common.def.vo.sys.excel.UserOperateLogExcelVO;
import com.bsj.power.common.mapper.UserOperateLogMapper;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.system.service.UserOperateLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description UserOperateLogServiceImpl
 * @time 2024/10/17 17:34
 */
@Service
public class UserOperateLogServiceImpl implements UserOperateLogService {

    /**
     * 操作类型与操作模块数值对应的中文类型
     */
    static final Map<Integer, String> operateModuleMap, operateTypeMap;

    static {
        operateModuleMap = Map.of(NumericalConstants.ONE, "用户", NumericalConstants.TWO, "角色");
        operateTypeMap = Map.of(NumericalConstants.ONE, "新增", NumericalConstants.TWO, "修改", NumericalConstants.THREE, "删除");
    }

    @Resource
    private UserOperateLogMapper userOperateLogMapper;

    /**
     * 用户导出记录分页
     *
     * @param userOperateLogSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.sys.UserOperateLogVO>>
     * @author ljx
     * @time 2024/10/17 19:04
     */
    @Override
    public JsonResult<List<UserOperateLogVO>> page(UserOperateLogSearchDTO userOperateLogSearchDTO) {
        IPage<UserOperateLog> page = new Page<>(userOperateLogSearchDTO.getPageNumber(), userOperateLogSearchDTO.getPageSize());
        QueryWrapper<UserOperateLog> queryWrapper = new QueryWrapper<>();
        String ip = userOperateLogSearchDTO.getIp();
        String userName = userOperateLogSearchDTO.getUserName();
        String startTime = userOperateLogSearchDTO.getStartTime();
        String endTime = userOperateLogSearchDTO.getEndTime();
        if (StringUtils.isNotBlank(userName)) {
            queryWrapper.lambda().like(UserOperateLog::getUserName, userName);
        }
        if (StringUtils.isNotBlank(ip)) {
            queryWrapper.lambda().like(UserOperateLog::getIp, ip);
        }
        if (!StringUtils.isAllBlank(startTime, endTime)) {
            queryWrapper.lambda().between(UserOperateLog::getCreateTime, startTime, endTime);
        }
        queryWrapper.lambda().orderByDesc(UserOperateLog::getCreateTime);
        List<UserOperateLogVO> userOperateLogVOS = new ArrayList<>();
        IPage<UserOperateLog> iPage = userOperateLogMapper.selectPage(page, queryWrapper);
        List<UserOperateLog> userOperateLogs = iPage.getRecords();
        if (!CollectionUtils.isEmpty(userOperateLogs)) {
            userOperateLogVOS = userOperateLogs.stream().map(userOperateLog -> {
                UserOperateLogVO userOperateLogVO = new UserOperateLogVO();
                BeanUtils.copyProperties(userOperateLog, userOperateLogVO);
                return userOperateLogVO;
            }).collect(Collectors.toList());
        }
        return JsonResult.success(userOperateLogVOS, iPage.getTotal());
    }

    /**
     * 获取用户操作记录导出数据
     *
     * @param userOperateLogSearchDTO
     * @return java.util.List<com.bsj.power.common.def.vo.sys.excel.UserOperateLogExcelVO>
     * @author ljx
     * @time 2024/10/17 19:05
     */
    @Override
    public List<UserOperateLogExcelVO> getExportData(UserOperateLogSearchDTO userOperateLogSearchDTO) {
        QueryWrapper<UserOperateLog> queryWrapper = new QueryWrapper<>();
        String ip = userOperateLogSearchDTO.getIp();
        String userName = userOperateLogSearchDTO.getUserName();
        String startTime = userOperateLogSearchDTO.getStartTime();
        String endTime = userOperateLogSearchDTO.getEndTime();
        if (StringUtils.isNotBlank(userName)) {
            queryWrapper.lambda().like(UserOperateLog::getUserName, userName);
        }
        if (StringUtils.isNotBlank(ip)) {
            queryWrapper.lambda().like(UserOperateLog::getIp, ip);
        }
        if (!StringUtils.isAllBlank(startTime, endTime)) {
            queryWrapper.lambda().between(UserOperateLog::getCreateTime, startTime, endTime);
        }
        queryWrapper.lambda().orderByDesc(UserOperateLog::getCreateTime);
        List<UserOperateLogExcelVO> userOperateLogExcelVOS = new ArrayList<>();
        List<UserOperateLog> userOperateLogs = userOperateLogMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(userOperateLogs)) {
            userOperateLogExcelVOS = userOperateLogs.stream().map(userOperateLog -> {
                UserOperateLogExcelVO userOperateLogExcelVO = new UserOperateLogExcelVO();
                BeanUtils.copyProperties(userOperateLog, userOperateLogExcelVO);
                userOperateLogExcelVO.setOperateType(operateTypeMap.get(userOperateLog.getOperateType()));
                userOperateLogExcelVO.setOperateModule(operateModuleMap.get(userOperateLog.getOperateModule()));
                userOperateLogExcelVO.setUserType(userOperateLog.getUserType().equals(NumericalConstants.ZERO) ? "系统用户" : "门户系统用户");
                userOperateLogExcelVO.setCreateTime(DateUtils.dateToStr(userOperateLog.getCreateTime()));
                return userOperateLogExcelVO;
            }).collect(Collectors.toList());
        }
        return userOperateLogExcelVOS;
    }
}
